<?php
/**
 * 好友管理
 * 添加
 * 删除
 * 获取列表
 */
namespace App\Service;

use App\Model\Friend;
use App\Model\Msg;
use App\ServiceInterface;

class FriendService implements ServiceInterface
{
    private $_server;

    private $_fd;

    private $_data;

    /**
     * BindService constructor.
     * @param null $server
     * @param int $fd
     * @param array $data
     */
    public function __construct($server=null,int $fd=0,array $data=[])
    {
        $this->_server = $server;
        $this->_fd     = $fd;
        $this->_data   = $data;
    }

    /**
     * 参数校验
     * @return string
     */
    public function check():string
    {
        // 判断行为动作
        if (!isset($this->_data['action']) || empty($this->_data['action']))
        {
            return 'action is null';
        }

        return '';
    }

    /**
     * 具体逻辑
     * @return array
     */
    public function init():array
    {
        try {
            $check_msg = $this->check();
            if ($check_msg)
                throw new \Exception($check_msg);

            switch ($this->_data['action'])
            {
                case 'add':
                    $res_data = $this->addFriend();
                    break;
                case 'agree':
                    $res_data = $this->agreeFriend();
                    break;
                case 'del':
                    $res_data = $this->delFriend();
                    break;
                case 'getOne':
                    $res_data = $this->getOneFriend();
                    break;
                case 'getList':
                    $res_data = $this->getListFriend();
                    break;
                case 'list':
                    $res_data = $this->listFriend();
                    break;
                case 'getFriedMsg':
                    $res_data = $this->getFriedMsg();
                    break;
                default:
                    $res_data = 'action not defind';
                    break;
            }
            if (!is_array($res_data))
                throw new \Exception($res_data);

            return [0, 'success', $res_data];

        }catch (\Exception $e){

            return [-1, $e->getMessage(), $this->_data];
        }
    }

    /**
     * 添加好友
     * @return string
     */
    private function addFriend(): string
    {
        return Friend::add($this->_data['appid'],$this->_data);
    }

    /**
     * 被添加后同意
     * @return string
     */
    private function agreeFriend()
    {
        return Friend::agree($this->_data['appid'],$this->_data);
    }

    /**
     * 删除好友
     * @return string
     */
    private function delFriend()
    {
        return Friend::del(
            $this->_data['appid'],
            $this->_data['account'],
            $this->_data['account_fid']
        );
    }

    /**
     * @return mixed
     */
    private function listFriend()
    {
        return Friend::getList($this->_data['appid'],$this->_data);
    }


    private function getOneFriend()
    {
        return Friend::getOne($this->_data['appid'],$this->_data);
    }

    /**
     * @return array|string
     */
    private function getFriedMsg()
    {
        return Msg::getFriedMsg($this->_data['appid'], $this->_data);
    }


}